﻿using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.interface2
{
    public partial class index : ModulePage
	{

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        
        protected void btnLogoImg_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("/images/interface/" + this.ViewState["LogoImg"].ToString());
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, "UPDATE [dbo].[Interface] SET [LogoImg]=NULL");
            this.fileLogoImg.Visible = true;
            this.litLogoImg.Text = "";
            this.btnLogoImg.Visible = false;
            InterfaceCache.Update();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Upload upload = new Upload();
            upload.AutoCreateDirectory = true;
            upload.OverwriteFile = true;
            upload.DestinationPath = Server.MapPath("/images/interface/");
            if (this.fileLogoImg.Visible && (this.fileLogoImg.PostedFile.ContentLength > 0))
            {
                if (!this.ImageType(this.fileLogoImg.PostedFile.ContentType))
                {
                    this.litMessage.Text = base.ShowPrompt("主界面-顶部图标格式不正确！", "index.aspx");
                    this.pnlMain.Visible = false;
                    return;
                }
                upload.GetFile(this.fileLogoImg.PostedFile);
                string fileName = upload.FileName;
                upload.DoUpload();
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@LogoImg", SqlDbType.NVarChar, 200) };
                commandParameters[0].Value = fileName;
                SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, "UPDATE [dbo].[Interface] SET [LogoImg] = @LogoImg", commandParameters);
            }
            InterfaceDao dao = new InterfaceDao();
            InterfaceData data = new InterfaceData();
            data.IeTitle = this.txtIeTitle.Text;
            data.StatusText = this.txtStatusText.Text;
            data.BannerText = this.txtBannerText.Text;
            data.BannerFont = this.txtBannerFont.Text;
            data.LogoHeight = Convert.ToInt32(this.txtLogoHeight.Text);
            data.LogoWidth = Convert.ToInt32(this.txtLogoWidth.Text);
            dao.Update(base.Conn, data);
            InterfaceCache.Update();
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("界面设置已保存！", "index.aspx");
        }
        private bool ImageType(string contentType)
        {
            switch (contentType)
            {
                case "image/gif":
                case "image/jpeg":
                case "image/pjpeg":
                case "image/bmp":
                case "image/x-png":
                case "image/x-ms-bmp":
                    return true;
            }
            return false;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
       
                DataTable table = new InterfaceDao().SelectAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    this.txtIeTitle.Text = (table.Rows[0]["IeTitle"] != DBNull.Value) ? table.Rows[0]["IeTitle"].ToString() : "";
                    this.txtBannerText.Text = (table.Rows[0]["BannerText"] != DBNull.Value) ? table.Rows[0]["BannerText"].ToString() : "";
                    if (table.Rows[0]["BannerFont"] != DBNull.Value)
                    {
                        this.txtBannerFont.Text = table.Rows[0]["BannerFont"].ToString();
                    }
                    this.txtStatusText.Text = (table.Rows[0]["StatusText"] != DBNull.Value) ? table.Rows[0]["StatusText"].ToString() : "";
                    this.txtLogoWidth.Text = (table.Rows[0]["LogoWidth"] != DBNull.Value) ? table.Rows[0]["LogoWidth"].ToString() : "100";
                    this.txtLogoHeight.Text = (table.Rows[0]["LogoHeight"] != DBNull.Value) ? table.Rows[0]["LogoHeight"].ToString() : "40";
                    if (table.Rows[0]["LogoImg"] != DBNull.Value)
                    {
                        this.ViewState["LogoImg"] = table.Rows[0]["LogoImg"].ToString();
                        this.litLogoImg.Text = "<a target='_blank' href='/images/interface/" + table.Rows[0]["LogoImg"].ToString() + "'>" + table.Rows[0]["LogoImg"].ToString() + "</a>";
                        this.fileLogoImg.Visible = false;
                        this.btnLogoImg.Visible = true;
                    }
                }
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }
        }

	}
}
